// pages/login/mobile/bind.js
import {
  bindMobile
} from '../../../api/user.js';
import {
  $wuxDialog
} from '../../../miniprogram_npm/wux-weapp/index';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.login({
      success: res => {
        this.data.code = res.code;
      }
    })
  },

  /**
   * 授权手机号
   */
  onBindPhone(e) {
    console.log(e)
    if (e.detail.errMsg != 'getPhoneNumber:ok') {
      return
    }
    const _oTemp = {
      code: this.data.code,
      memberId: getApp().globalData.memberId,
      mobile: {
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData
      }
    };

    this.bindMobile(_oTemp);
  },

  /**
   * 绑定手机号
   * @param obj 授权内容
   */
  bindMobile(obj) {
    bindMobile(obj).then(() => {
      $wuxDialog().alert({
        maskClosable: false,
        title: '注册成功',
        content: '首次进入需填写一些您的个人资料',
        confirmText: '去填写',
        onConfirm(e) {
          wx.redirectTo({
            url: '/pages/login/information/index',
          })
        },
      })
    })
  }
})